<?php
include_once(APP_PATH.'/sms/configs/sms_config.php');
class sms_model_data extends Model {
	const PAGE_SIZE=25;	
		
	public function __construct(){
		parent::__construct();
		$this->table = 'app_sms_ms';
		$this->reltable = 'app_sms_session';
	}
	
	public function setTables($history=false){
		if ($history===true) {
			$this->table = 'app_sms_ms_backup';
			$this->reltable = 'app_sms_session_backup';
		}
		return ;
	}
	/**
	 * 发送短消息接口：
	 *
	 * @param array $args
	 * @return int ;
	 */
	public function add($args){ 
//		$args = filter_params($args,'sms_title','sms_content','sms_senderid','sms_pid','sms_sender','sms_reciverid','sms_reciver');
		trim_params($args);
		//stripped all html and php tags
		$args['sms_content'] = strip_tags($args['sms_content'],'<img>');
		$msid = false;
		$args['sms_dateline'] = time();
		$msid = $this->db->insert($this->table, $args);
		if ($msid) {
			if(is_numeric($args['sms_pid'])){
				$sql = 'UPDATE '.$this->table.'  a,(SELECT sms_total_num FROM '.$this->table .'  WHERE sms_pid = '.$args['sms_pid'].' LIMIT 1 ) b SET a.sms_total_num=b.sms_total_num+1 WHERE sms_id = '.$args['sms_pid'].' OR sms_pid='.$args['sms_pid'];
				//$sql = 'UPDATE '.$this->table.' a SET a.sms_total_num=a.sms_total_num+1 WHERE a.sms_id = '.$args['sms_pid'].' OR a.sms_pid='.$args['sms_pid'];
			}else {
				$sql = 'UPDATE '.$this->table.'  a SET a.sms_pid='.$msid.' WHERE  a.sms_id='.$msid;
			}
			$this->db->query($sql);
		}
		return $msid;
	}
	
	/**
	 * 发送短消息-添加会话关系接口
	 *
	 * @param unknown_type $args
	 * @return unknown
	 */
	public function addRel($args,$newPm=true){
//		$sender = $args['sender'];
		$args = filter_params($args, 'sms_id', 'sms_uid','sms_username', 'sms_type', 'sms_last_msid', 'sms_lastsend_msid');
		trim_params($args);
		$msid = false;
		$msid = $this->db->insert($this->reltable, $args);
		if($newPm!==false)
			$this->updateSmsNums($args,sms_config::MSG_SMS_CNT);
		return $msid;
	}
	
	/**
	 * 更新未读短消息数
	 *
	 * @param unknown_type $param
	 * @param unknown_type $type
	 */
	private  function updateSmsNums(&$param,$type=1){
		if(is_array($param['sms_uid']) && !empty($param['sms_uid'])){
			foreach ($param['sms_uid'] as $v){
				$$this->updateSmsCount($v,$type,1);
			}
		}else{
			$this->updateSmsCount($param['sms_uid'],$type,1);
		}
		unset($data);
		return ;
	}
	
	/**
	 * 更新最后收件人的消息id
	 *
	 * @param unknown_type $msg_id
	 * @param unknown_type $sms_id
	 * @param unknown_type $uid int or array
	 * @return unknown
	 */
	public function updateLastReciveSms($msg_id,$sms_id,$uid){
		if (is_array($uid) && !empty($uid)) {
			$uid = implode(',',$uid);
		}
		$sql = 'update '.$this->reltable.' set sms_last_msid=&msg_id where sms_id=&sms_id and sms_uid IN ('.$uid.')';
		$params = array(
			'msg_id'=>$msg_id,
			'sms_id'=>$sms_id
		);
		return $this->db->query($sql, $params);
	}
	
	/**
	 * 更新最后发送的短消息id
	 *
	 * @param unknown_type $msg_id
	 * @param unknown_type $sms_id
	 * @param unknown_type $uid
	 * @return unknown
	 */
	public function updateLastSendSms($msg_id,$sms_id,$uid){
		$sql = 'update '.$this->reltable.' set sms_lastsend_msid=&msg_id where sms_id=&sms_id and sms_uid=&uid';
		$params = array(
			'msg_id'=>$msg_id,
			'sms_id'=>$sms_id,
			'uid'=>$uid
		);
		return $this->db->query($sql, $params);
	}
	/**
	 * 删除发件箱内容
	 *
	 * @param int $msid  短消息id
	 * @param int $uid   会员id
	 * @param boolean $isAll
	 * @return boolean
	 */
	public function delSendSms($uid,$msid=''){
		if(!$msid){
			$sql = 'UPDATE '.$this->table.' SET sms_isdelete = 1 WHERE  sms_senderid=&uid';
		}
		else {
			if(is_array($msid) && !empty($msid)){
				$msid = implode(',',$msid);
			}
			$sql = 'UPDATE '.$this->table.' SET sms_isdelete = 1 WHERE sms_id IN ('.$msid.') AND sms_senderid=&uid';
		}
		$para = array('uid' => $uid);
		return $this->db->query($sql, $para);
	}
	
	/**
	 * 删除收件箱内容
	 *
	 * @param int $msid  短消息id
	 * @param int $uid   会员id
	 * @param boolean $isAll
	 * @return boolean
	 */
	public function delReciverSms($uid,$msid=''){
		if(!$uid) return false;
        $sql = '';
		if($msid){//删除某个或者某些短消息
			if(is_array($msid) && !empty($msid)){
				$msid = implode(',',$msid);
			}
			$sql .= ' UPDATE '.$this->reltable.' a ,(SELECT sms_pid FROM '.$this->table.' WHERE sms_id IN ('.$msid.') ) b SET a.sms_del=1,a.sms_lastdel_msid= CASE WHEN a.sms_last_msid>sms_lastsend_msid THEN sms_last_msid ELSE sms_lastsend_msid END
WHERE a.sms_id = b.sms_pid AND a.sms_uid=&uid';
		}else{//清空所有的收件箱
			$sql ='UPDATE app_sms_session a SET a.sms_del=1,a.sms_lastdel_msid = CASE WHEN a.sms_last_msid>sms_lastsend_msid THEN sms_last_msid ELSE sms_lastsend_msid END  WHERE a.sms_uid=&uid ';
		}
		$para = array('uid' => $uid);
		if($this->db->query($sql, $para)){
			$sql = ' UPDATE '.$this->table.' SET sms_isdelete=1 WHERE sms_senderid=&uid';
			if($msid) $sql.=' AND sms_pid IN ('.$msid.') OR sms_id IN ('.$msid.')';
			$this->db->query($sql, $para);
			return true;
		}
		return false;
	}
	
	/**
	 * 获取收件箱列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getOutBoxpage($uid,$page=1,$pageSize=PAGE_SIZE,$history=''){
		$tbname = $this->table;
		if(preg_match('/^\d{4}-\d{2}$/',$history)){
			$tbname .= '_'.str_replace('-','_',$history);
		}
		$sql = 'SELECT a.sms_id,a.sms_title,a.sms_content,a.sms_sender,a.sms_senderid,a.sms_reciver,a.sms_reciverid,a.sms_recive_num,a.sms_pid,a.sms_total_num,a.sms_duplicate,a.sms_dateline  
		FROM '.$tbname.' a WHERE sms_senderid=&uid AND sms_isdelete=0 ORDER BY sms_id DESC';
		$params = array('uid'=>$uid);
		return $this->db->selectPage($sql, $params, $page, $pageSize );
	}
	
	
	/**
	 * 获取收件箱列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getInBoxpage($uid,$page=1,$pageSize=PAGE_SIZE,$history=''){
		$tbname_sms = $this->table;
		$tbname_session = $this->reltable;
		if(preg_match('/^\d{4}-\d{2}$/',$history)){
			$tbname_sms .= '_'.str_replace('-','_',$history);
			$tbname_session .= '_'.str_replace('-','_',$history);
		}
		$sql = 'SELECT a.sms_id,a.sms_title,a.sms_content,a.sms_sender,a.sms_senderid,a.sms_pid,a.sms_total_num,a.sms_dateline ,
				b.sms_type,b.sms_lastdel_msid,b.sms_username,b.sms_readed,b.sms_uid
		FROM '.$tbname_sms.' a,'.$tbname_session.' b 
		WHERE b.sms_uid = &uid AND a.sms_senderid!=&uid AND b.sms_last_msid = a.sms_id AND b.sms_last_msid>b.sms_lastdel_msid ORDER BY b.sms_last_msid DESC ';
		$params = array('uid'=>$uid);
		return $this->db->selectPage($sql, $params, $page, $pageSize );
	}
	
	/**
	 * 获取会话中会员的相关会话信息
	 *
	 * @param unknown_type $sms_id
	 * @param unknown_type $uid
	 * @return unknown
	 */
	public function getReciveSmsInfo($sms_id,$uid=''){
		$sql = 'SELECT * FROM '.$this->reltable.' a WHERE  a.sms_id=&sms_id ';
		if($uid){
			if(is_array($uid) && !empty($uid)) $uid = implode(',',$uid);
			$sql.=' AND a.sms_uid IN ('.implode(',',$uid).') ';
		}
		$para = array('sms_id'=>$sms_id);
		return $this->db->fetchAll($sql, $para);
	}
	
	/**
	 * 根据消息id 获取参与会话的列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $self
	 */
	public  function getReciverByUid($sms_id,$self=''){
		if ($self) {
			$sql = 'SELECT sms_uid AS uid,sms_username AS username,sms_del,sms_readed,sms_last_msid,sms_lastsend_msid,sms_lastdel_msid FROM '.$this->reltable. ' WHERE sms_id=&sms_id AND sms_uid!='.$self;
		}
		else $sql = 'SELECT sms_uid AS uid,sms_username AS username,sms_del,sms_readed,sms_last_msid,sms_lastsend_msid,sms_lastdel_msid FROM '.$this->reltable. ' WHERE sms_id=&sms_id ';
		$para = array('sms_id'=>$sms_id);
		return $this->db->fetchAll($sql, $para);
	}
	
	/**
	 * 获取某一会话的所有短信息
	 *
	 * @param unknown_type $sms_id
	 * @return unknown
	 */
	public function getSmsInfoById($sms_id){
		//$sql = 'SELECT a.sms_id,a.sms_title,a.sms_content,a.sms_sender,a.sms_senderid,a.sms_reciver,a.sms_reciverid,a.sms_pid,a.sms_total_num,a.sms_duplicate,a.sms_dateline,a.sms_isdelete  FROM '.$this->table.' a WHERE a.sms_id =&sms_id OR a.sms_pid = &sms_id ORDER BY a.sms_id ';
		$sql = 'SELECT a.sms_id,a.sms_title,a.sms_content,a.sms_sender,a.sms_senderid,a.sms_reciver,a.sms_reciverid,a.sms_pid,a.sms_total_num,a.sms_duplicate,a.sms_dateline,a.sms_isdelete  FROM '.$this->table.' a WHERE  a.sms_pid = &sms_id ORDER BY a.sms_id ';
		$para = array('sms_id' => $sms_id);		
		return $this->db->fetchAll($sql,$para);
	}
	
	/**
	 * 更新短消息接收人数量
	 * Enter description here ...
	 * @param unknown_type $sms_id  短消息id
	 * @param unknown_type $num  消息接收人数量
	 */
	public function upSmsNums($sms_id,$num=1){
		if(!is_numeric($sms_id) || !is_numeric($num)) return false;
		$sql = 'UPDATE '.$this->table.' SET sms_recive_num = '.$num.' WHERE sms_id='.$sms_id;
		return $this->db->query($sql);
	}
	
	/**
	 * 获取单条短消息信息
	 *
	 * @param unknown_type $sms_id
	 * @return unknown
	 */
	public function select($sms_id){
		$sql = "SELECT *  FROM ".$this->table." a WHERE a.sms_id=&id";
		$params = array('id'=>$sms_id);
		return $this->db->fetchRow($sql, $params);
	}
	
	public function updateCount($sms_id){
		$sql = "update ".$this->table." set message_count=message_count+1 where id=&sms_id";
		$params = array('sms_id'=>$sms_id);
		return $this->db->query($sql, $params);
	}
	
	public function  getArchCategory($type=1){
		$sql = 'SELECT arch_year,arch_month FROM app_sms_archive_log WHERE arch_type=&type';
		$params = array('type'=>$type);
		return $this->db->fetchAll($sql, $params);
	}
	/**
	 * 更新未读短消息
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $type
	 * @param unknown_type $cnt
	 * @return unknown
	 */
	public function updateSmsCount($uid,$type,$cnt = 1){
		if(!$uid) return false;
		if(is_array($uid) && !empty($uid)){
			$uid = implode(',',$uid);
		}
		$sql = '';
		!$cnt && $cnt = 0;
		switch ($type){
			case sms_config::MSG_SMS_CNT :		//短消息数
				$sql.='INSERT INTO app_sms_count(uid,asc_sms) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt!==0 )?(' asc_sms=asc_sms+'.intval($cnt) ):(' asc_sms=0' );
				break;
			case sms_config::MSG_SYS_SMS_CNT :	//系统消息数
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(' asc_sysann=asc_sysann+'.intval($cnt) ):(' asc_sysann=0,asc_friends=0,asc_gift=0,asc_buff=0,asc_cite=0 ' );
				break;
			case sms_config::MSG_SYS_ANNO_CNT :	//系统公告
				$sql.='INSERT INTO app_sms_count(uid,asc_announce) VALUES ('.$uid.','.$cnt.') on DUPLICATE KEY UPDATE asc_announce='.intval($cnt);
				break;
			case sms_config::MSG_MESSAGE_CNT :	//留言数
				$sql.='INSERT INTO app_sms_count(uid,asc_message) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(' asc_message=asc_message+'.intval($cnt) ):(' asc_message=0' );
				break;
			case sms_config::MSG_COMMENT_CNT :	//评论数
				$sql.='INSERT INTO app_sms_count(uid,asc_comment) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(' asc_comment=asc_comment+'.intval($cnt) ):(' asc_comment=0' );
				break;
			case sms_config::MSG_SYS_FRIENDS :	//好友请求数
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_friends= asc_friends+$cnt,asc_sysann=asc_sysann+$cnt"):(' asc_sysann=asc_sysann-asc_friends,asc_friends=0' );
				break;
			case sms_config::MSG_SYS_GIFT  :	//礼物数
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_gift= asc_gift+$cnt,asc_sysann=asc_sysann+$cnt"):(' asc_sysann=asc_sysann-asc_gift,asc_gift=0' );
				break;
			case sms_config::MSG_SYS_BUFF  :	//技能数
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_buff=asc_buff+$cnt,asc_sysann=asc_sysann+$cnt "):(' asc_sysann=asc_sysann-asc_buff,asc_buff=0 ' );
				break;
			case sms_config::MSG_SYS_CITE  :	//转帖数
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_cite=asc_cite+$cnt,asc_sysann=asc_sysann+$cnt "):(' asc_sysann=asc_sysann-asc_cite,asc_cite=0 ' );
				break;
			case sms_config::MSG_RECOM_CNT  :	//评论回复数
				$sql.='INSERT INTO app_sms_count(uid,asc_recomment) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_recomment=asc_recomment+$cnt  "):(' asc_recomment=0 ' );
				break;
			case sms_config::MSG_REMSG_CNT  :	//留言回复数
				$sql.='INSERT INTO app_sms_count(uid,asc_remessage) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_remessage=asc_remessage+$cnt "):(' asc_remessage=0 ' );
				break;
			case sms_config::MSG_DKP_CNT  :		//DKP数
				$sql.='INSERT INTO app_sms_count(uid,asc_dkp) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_dkp=asc_dkp+$cnt "):(' asc_dkp=0 ' );
				break;
			case sms_config::MSG_FICTION_CNT  :		//小说数
				$sql.='INSERT INTO app_sms_count(uid,fiction) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" fiction=fiction+$cnt "):(' fiction=0 ' );
				break;
			case sms_config::MSG_CARTOON_CNT  :		//漫画数
				$sql.='INSERT INTO app_sms_count(uid,asc_cartoon) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_cartoon=asc_cartoon+$cnt "):(' asc_cartoon=0 ' );
				break;
			case sms_config::MSG_ANIMATION_CNT  :		//动画数
				$sql.='INSERT INTO app_sms_count(uid,animation) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" animation=animation+$cnt "):(' animation=0 ' );
				break;
			default:
				return false;
				break;
		}
		if($sql && $this->db->query($sql)){
			$this->queryUnReadSms($uid,true);//跟新缓存
			return true;
		}
		return false;
	}
	/**
	 * 清空未读短消息
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $type
	 * @return unknown
	 */
	public function cleanSmsCount($uid,$cnt = 0){
		$unReadNum = $this->queryUnReadSms($uid);
		if($unReadNum['smsnum']>0)//如果有未读消息
			return self::updateSmsCount($uid,sms_config::MSG_SMS_CNT ,$cnt);
		return false;
	}
	
	/**
	 * 清空系统未读短消息数
	 *
	 * @param unknown_type $uid
	 * @return unknown
	 */
	public function cleanSysSmsCount($uid){
		$unReadNum = $this->queryUnReadSms($uid);
		if($unReadNum['sysnum']>0)//如果有未读消息
		return self::updateSmsCount($uid,sms_config::MSG_SYS_SMS_CNT,0);
	}
	/**
	 * 设置消息为已读
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $type true:表示设置已读短消息 false 表示未读短消息
	 * @return unknown
	 */
	public function setReaded($uid,$type=true){
		if(!is_numeric($uid)) return false;
		$para = array('uid' => $uid);
		if($type==false){//set system notice readed
			$sql = 'UPDATE  '.sms_util_system::getTableName($uid).'  SET readed=1 WHERE recieverid=&uid AND readed=0';
		}else {
			$sql = 'UPDATE '.$this->reltable.' SET sms_readed=1 WHERE sms_uid=&uid AND sms_readed=0 ';
		}
		return $this->db->query($sql,$para);
	}
	
	/**
	 * 设置消息为已读
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $type true:表示设置已读短消息 false 表示未读短消息
	 * @return unknown
	 */
	public function setReadedById($uid,$id){
		if(!is_numeric($uid)) return false;
		$para = array('uid' => $uid);
		$sql = 'UPDATE '.$this->reltable.' SET sms_readed=1 WHERE sms_id='.$id.' AND sms_uid=&uid AND sms_readed=0 ';
		return $this->db->query($sql,$para);
	}
	
	public function queryUnReadSms($uid,$flush = false){
		if(!is_numeric($uid)) return false;
		$key = CACHE_KEY_NEW_SMS_PRE.$uid;//缓存未读消息数的Memcache$key
		if(!($rst = MemRow::getCache($key)) || $flush==true){
			$sql = "SELECT asc_sms as smsnum,asc_sysann as sysnum ,asc_announce as announcement, asc_comment as comnum,asc_recomment as recomment,asc_message as msgnum,asc_remessage as remsgnum,asc_friends as friendsnum,asc_buff as buffnum,asc_gift as giftnum,asc_cite AS citenum,asc_dkp AS dkpnum,asc_cartoon AS cartoon,animation AS anim,fiction AS fiction FROM app_sms_count WHERE uid=&uid ";
			$params = array('uid' =>$uid);
			if($rst = $this->db->fetchRow($sql, $params)){
				MemRow::mset($key,$rst,0);
				return $rst;
			}
		}
		
		return $rst;
	}
	
	public function updateTarget($id, $target){
		$sql = "update ".$this->table." set target=&target where id=&id" ;
		$target['id'] = $id;
		$params = array(
			'id'=>$id,
			'target'=>serialize($target)
		);
		$this->db->query($sql, $params);
	}
	
	
	
	public function rollback($sms_id,$type=true){
		$sql = "delete from ".($type?$this->table:$this->reltable)." where sms_id=&sms_id";
		$params = array('sms_id'=>$sms_id);
		return $this->db->query($sql, $params);
	}
	
	/**
	 * 获取短消息列表（管理后台使用）
	 * Enter description here ...
	 * @param unknown_type $uid 用户uid,如果为空，则列出所有
	 * @param unknown_type $page  当前页
	 * @param unknown_type $pageSize 每页显示数
	 * @param unknown_type $start_time 搜索开始时间
	 * @param unknown_type $end_time  搜索结束时间
	 */
	public function getSmsList($uid='',$page=1,$pageSize=20,$type ,$start_time='',$end_time='',$delete = ''){
		if(is_array($uid) && !empty($uid)){
			$uid = implode(',',$uid);
		}
		$where = '';
		if($start_time){
			$st = date_parse($start_time);
			$starttime =gmmktime($st['hour'], $$st['minute'], $st['second'], $st['month'], $st['day'], $st['year']);
			if($end_time){
				$st = date_parse($end_time);
				$endtime =gmmktime($st['hour'], $$st['minute'], $st['second'], $st['month'], $st['day'], $st['year']);
			}else
				$endtime = time();
		}
		$where .= $uid?' sms_senderid='.$uid:'';
		$where.= $start_time?($where?' AND ':' ').' sms_dateline>='.$starttime.' AND sms_dateline<='.$endtime:'';
		$where.=($type)?($where?' AND ':' ').' sms_isdelete<1 AND sms_verify='.$type:'';
		$where.=($delete)?($where?' AND ':' ').' sms_isdelete = 4  ':'';
		$sql  = 'SELECT * FROM '.$this->table.(trim($where)?' WHERE '.$where:' ');
		$sql.=' ORDER BY sms_id DESC ';
		return $this->db->selectPage($sql,array(), $page, $pageSize);
	}
	
	/**
	 * 屏蔽短信
	 * Enter description here ...
	 * @param unknown_type $sms_id 消息id，可以为 数字、数字和以“,”分割的id
	 * @param unknown_type $uid  用户id 
	 * @param unknown_type $delete 为TRUE表示删除，FALSE表示屏蔽
	 */
	public function invalidSmsById($sms_id = '',$uid='',$delete = false){
		$where = $whererel = '';
		if(!$sms_id && !$uid) return false;
		if($sms_id) {
			$sms_id = is_array($sms_id)?implode(',',$sms_id):$sms_id;
			$where .= ' sms_id IN ('.$sms_id.') ';
			$whererel = $delete?$where:'';
		}
		if($uid){
			$where = '';
			$uid = is_array($uid)?implode(',',$uid):$uid;
			$where .= ' sms_senderid IN ('.$uid.') ';
			$whererel = $delete?' EXISTS (SELECT 1 FROM '.$this->table.' b WHERE  b.sms_uid IN ('.$uid.') AND a.sms_id = b.sms_id ':'';
		}
		if($delete==true){			
			$sql = 'DELETE FROM '.$this->reltable.'  WHERE '.$whererel;
			if($this->db->query($sql)){
				return $this->db->query('DELETE FROM '.$this->table.' WHERE '.$where);
			}
		}else{
			return $this->db->query('UPDATE '.$this->table.' SET sms_isdelete=4,sms_uptime='.time().' WHERE '.$where);
		}
//		$sql = 'UPDATE '.$this->table.' SET sms_isdelete=4 WHERE '.$where;
//		return $this->db->query($sql);
	}
	
	public function verifySms($sms_id,$uid=''){
		if(!$sms_id && !$uid) return false;
		$where = '';
		if($sms_id){
			$sms_id = is_array($sms_id)?implode(',',$sms_id):$sms_id;
			$where .= ' sms_id IN ('.$sms_id.') ';
		}
		if($uid && is_numeric($uid)){
			$where = '';
			$where .= ' sms_senderid IN ('.$uid.') ';
		}
		$sql = 'UPDATE '.$this->table.' SET sms_verify=1,sms_uptime='.time().' WHERE '.$where;
		return $this->db->query($sql);
	}
	
	/**
	 * 恢复被删除的短信
	 * Enter description here ...
	 * @param unknown_type $sms_id
	 */
	public function resumeSms($sms_id){
		if(!$sms_id) return false;
        $where = '';
		$sms_id = is_array($sms_id)?implode(',',$sms_id):$sms_id;
		$where .= ' sms_id IN ('.$sms_id.') ';		
		$sql = 'UPDATE '.$this->table.' SET sms_isdelete=0 ,sms_uptime='.time().' WHERE '.$where;
		return $this->db->query($sql);
	}
	
}